/*
 * @Author: Starfish_DL
 * @Date: 2020-09-13 10:13:23
 * @LastEditTime: 2020-09-23 13:16:34
 * @LastEditors: Starfish_DL
 * @Description: Finish homework of 0913 using C.
 * @FilePath: \undefinedc:\vscode\cpp\0913.cpp
 */

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 50

void swap(int &a,int &b);

typedef struct CustomList
{
    int data[MAXSIZE];
    int length;
}SqList;

void CreateList(SqList *&L,int target[],int n)
{
    int i=0,k=0;
    L=(SqList *)malloc(sizeof(SqList));
    while (i<n)
    {
        L->data[k]=target[i];
        k++;
        i++;
    }
    L->length=k;
}

void DeleteList(SqList *L)
{
    free(L);
}

void DispList(SqList *L)
{
    for (int i = 0; i < L->length; i++)
    {
        printf("%d ",L->data[i]);
    }
    printf("\n");
}

void partition_1(SqList *L)
{
    int i=1,j=L->length-1;
    int pivot=L->data[0];
    while (i<j)
    {
        while (i<j&&L->data[j]<pivot)
        {
            j--;
        }
        while (i<j&&L->data[i]>=pivot)
        {
            i++;
        }
        if (i<j)
        {
            swap(L->data[i],L->data[j]);
        }
    }
    swap(L->data[0],L->data[j]);
}

void swap(int &a,int &b)
{
    int temp;
    temp=b;
    b=a;
    a=temp;
}

int main()
{
    int a[]={3,8,2,7,1,5,3,0};
    SqList *L;
    CreateList(L,a,8);
    DispList(L);
    partition_1(L);
    DispList(L);
    DeleteList(L);
    return 0;
}